package com.xcity.tools.http.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Employee.State;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.employee.template.EmployeeTemplate;
import com.xcity.game.skill.SkillUtils;
import com.xcity.pomelo.response.CombatAttributeResponse;
import com.xcity.tools.http.HttpService;
import com.xcity.tools.http.HttpService.SheetField;
import com.xcity.tools.http.XLS2JSONFilter;

public class EmployeeTemplateFilter implements XLS2JSONFilter<EmployeeTemplate> {

	static Integer[] filterIds = new Integer[] {
			90001,
			90002,
			90003,
			90004,
			90005,
			90011,
			90012,
			90013,
			90014,
			90015,
			90023,
			90028,
			90201,
			90202,
			90203,
			90204,
			90205,
			90206,
			90207,
			90208,
			90209,
			90210,
			90211,
			90212,
			90213,
			90214,
			90215,
			90216,
			90217,
			90218,
			90219,
			90220,
			90221,
			90222,
			90223,
			90224,
			90225,
			90226,
			90227,
			90228,
			90229,
			90230,
	};
	static Set<Integer> filterIdsSet = new HashSet<>(Arrays.asList(filterIds));
	
	@Override
	public Object doFilter(EmployeeTemplate template, String field, Method getter) throws Exception {
//		fields.add(new SheetField("id", "员工id"));
		if ("id".equals(field)) {
			return template.getId();
		}
		if ("templateId".equals(field)) {
			return template.getId();
		}
//		fields.add(new SheetField("quality", "品质"));
		if ("quality".equals(field)) {
			return template.getQuality();
		}
//		fields.add(new SheetField("clazz", "职业"));
		if ("clazz".equals(field)) {
			return template.getClazz();
		}
//		fields.add(new SheetField("name", "姓名"));
		if ("name".equals(field)) {
			return template.getName();
		}
//		fields.add(new SheetField("sex", "性别"));
		if ("sex".equals(field)) {
			return template.getSex();
		}
//		fields.add(new SheetField("icon", "头像"));
		if ("icon".equals(field)) {
			return template.getIcon();
		}
//		
//		fields.add(new SheetField("level", "等级"));
		if ("level".equals(field)) {
			return 1;
		}
//		fields.add(new SheetField("qualityLvl", "品级"));
		if ("qualityLvl".equals(field)) {
			return 0;
		}
//		fields.add(new SheetField("starLvl", "星级"));
		if ("starLvl".equals(field)) {
			return 0;
		}
		Employee fake = getFakeEmployee(template);
//		fields.add(new SheetField("combatPower", "战斗力"));
		if ("combatPower".equals(field)) {
			return CombatUtils.calcCombatPower(fake);
		}
//		fields.add(new SheetField("attr", "战斗属性"));
		if ("attr".equals(field)) {
			return new CombatAttributeResponse(fake);
		}
//		fields.add(new SheetField("skills2", "技能列表"));
		if ("skills2".equals(field)) {
			return SkillUtils.toResponse(fake);
//			return Arrays.asList(new SkillV2Info(fake.getSkillManager().getActive()));
		}
		return null;
	}

	@Override
	public Object doFilterAll(List<EmployeeTemplate> templates, Set<String> fields) throws Exception {
		if (fields.contains("id")) {
			fields.add("templateId");
		}
		JSONArray jsonArr = new JSONArray(templates.size());
		for (EmployeeTemplate template : templates) {
			if (filterIdsSet.contains(template.getId())) {
				JSONObject jsonObj = new JSONObject(true);
				jsonArr.add(jsonObj);
				for (String field : fields) {
					Method m = HttpService.fetchGetterMethod(EmployeeTemplate.class, field);
					jsonObj.put(field, doFilter(template, field, m));
				}
			}
		}
		return jsonArr;
	}

	@Override
	public List<SheetField> getFields() {
		List<SheetField> fields = new ArrayList<>();
		fields.add(new SheetField("id", "员工id"));
		fields.add(new SheetField("quality", "品质"));
		fields.add(new SheetField("clazz", "职业"));
		fields.add(new SheetField("name", "姓名"));
		fields.add(new SheetField("sex", "性别"));
		fields.add(new SheetField("icon", "头像"));
		
		fields.add(new SheetField("level", "等级"));
		fields.add(new SheetField("qualityLvl", "品级"));
		fields.add(new SheetField("starLvl", "星级"));
		fields.add(new SheetField("combatPower", "战斗力"));
		fields.add(new SheetField("attr", "战斗属性"));
		fields.add(new SheetField("skills2", "技能列表"));
		return fields;
	}
	

	Map<Integer, Employee> fakes = new HashMap<>();
	private Employee getFakeEmployee(EmployeeTemplate template) {
		Employee e = fakes.get(template.getId());
		if (e == null) {
			e = new Employee();
			e.setId(template.getId());
			e.setTemplate(template);
			e.setLevel(1);
			e.setState(State.FREE);
//			e.setAttributes(new float[0]);
			e.setCombos(Collections.emptyList());
			e.setBlessLvls(new int[0]);
			e.doInit(null);
			fakes.put(template.getId(), e);
		}
		return e;
	}
	
}
